package com.arges.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arges.web.common.Constant;
import com.arges.web.common.DateUtil;
import com.arges.web.common.GsonUtils;
import com.arges.web.domain.bean.DocBean;
import com.arges.web.domain.pojo.Notice;
import com.arges.web.page.PageInfo;
import com.arges.web.service.NoticeManager;

/**
 * Notice控制层
 * 
 * @author zxj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/notice")
public class NoticeMyController
{

	@Autowired
	private NoticeManager noticeManager;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getPageNotice.do")
	public String getPageDoc(HttpServletRequest request, ModelMap map)
	{

		PageInfo<Notice> pageInfo = null;
		String title = request.getParameter("title");
		if (!StringUtils.isNotBlank(title))
		{
			title = "";
		}

		String pageString = request.getParameter("PageInfo");
		if (StringUtils.isNotBlank(pageString))
		{
			pageInfo = GsonUtils.stringToObject(pageString, PageInfo.class);
		}
		else
		{
			pageInfo = new PageInfo<Notice>();
		}

		try
		{
			pageInfo = noticeManager.getPageNotice(pageInfo, title);
		}
		catch (Exception e)
		{
			System.out.println("getPageDoc is error. e=" + e);
		}
		map.put("chaxun", title);
		map.put("NoticeList", pageInfo.getResult());
		pageInfo.setResult(null);
		map.put("noticePageInfo", GsonUtils.objectToJson(pageInfo));
		return "manager/notice";
	}

	@RequestMapping(value = "/save.do")
	@ResponseBody
	public String save(HttpServletRequest request, Notice notice)
	{
		String result = Constant.SUCCESS;
		String createDateString = request.getParameter("createDateString");
		try
		{
			if (StringUtils.isNoneBlank(createDateString))
			{
				notice.setCreateDate(DateUtil.convertString2Date(createDateString, "yyyy-MM-dd HH:mm:ss"));
			}
			DocBean docBean = (DocBean) request.getSession().getAttribute("currentUser");
			notice.setUserName(docBean.getName());
			noticeManager.save(notice);
		}
		catch (Exception e)
		{
			System.out.println("save notice is error,error is" + e);
			result = Constant.ERROR;
		}

		return result;
	}

	/**
	 * 根据ID获取用户信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findNoticeById.do")
	@ResponseBody
	public Notice findNoticeById(HttpServletRequest request)
	{
		String id = request.getParameter("id");
		Notice notice = null;
		try
		{
			notice = noticeManager.findNoticeById(Long.parseLong(id));
		}
		catch (Exception e)
		{
			System.out.println("findNoticeById is error,error is" + e);
		}
		return notice;
	}

	/**
	 * 删除、批量删除公告
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/deleteByIds.do")
	@ResponseBody
	public String deleteByIds(HttpServletRequest request)
	{
		String result = Constant.SUCCESS;
		String ids = request.getParameter("ids");
		try
		{
			noticeManager.deleteByIds(ids);
		}
		catch (Exception e)
		{
			System.out.println("deleteByIds notice is error,error is" + e);
			result = Constant.ERROR;
		}
		return result;
	}

}
